<template>
  <Form ref="loginForm"
        :model="form"
        :rules="rules"
        @keydown.enter.native="handleSubmit">
    <FormItem prop="userName">
      <Input v-model="form.userName"
             placeholder="请输入用户名">
      <span slot="prepend">
        <Icon :size="16"
              type="ios-person"></Icon>
      </span>
      </Input>
    </FormItem>
    <FormItem prop="password">
      <Input type="password"
             v-model="form.password"
             placeholder="请输入密码">
      <span slot="prepend">
        <Icon :size="14"
              type="md-lock"></Icon>
      </span>
      </Input>
    </FormItem>
    <!-- <FormItem prop="authCode">
          <Input  v-model="form.authCode"  placeholder="请输入验证码" style="width: 160px" />
          <div class="sendCode" @click="sendCode">{{content}}</div>
      </FormItem> -->
    <FormItem>
      <Button @click="handleSubmit"
              :loading="loading"
              type="primary"
              long>登录</Button>
    </FormItem>
  </Form>
</template>
<script>
import { sendEmailCode, sendPhoneCode } from '@/api/user'
export default {
  name: 'LoginForm',
  props: {
    userNameRules: {
      type: Array,
      default: () => {
        return [
          { required: true, message: '账号不能为空', trigger: 'blur' }
        ]
      }
    },
    loading: {
      type: Boolean
    },
    passwordRules: {
      type: Array,
      default: () => {
        return [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ]
      }
    }
    // authCodeRules: {
    //   default: () => {
    //     return [
    //       { required: true, message: '验证码不能为空', trigger: 'blur' }
    //     ]
    //   }
    // }
  },
  data () {
    return {
      form: {
        userName: '',
        password: '',
        // authCode: '',
        loading: false
      },
      content: '点击获取',
      totalTime: 59,
      canClick: true
    }
  },
  computed: {
    rules () {
      return {
        userName: this.userNameRules,
        password: this.passwordRules
        // authCode: this.authCodeRules
      }
    }
  },
  methods: {
    handleSubmit () {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.$emit('on-success-valid', {
            userName: this.form.userName,
            password: this.form.password
          })
        }
      })
    },
    sendCode () {
      if (!this.canClick) return
      this.canClick = false
      this.content = `${this.totalTime}s后重新发送`
      if (this.form.userName.indexOf('@') === -1) {
        sendPhoneCode({ countryCode: '86', phone: this.form.userName }).then(res => {
          this.countDown()
          this.$Message('发送成功')
        })
      } else {
        sendEmailCode({ email: this.form.userName }).then(res => {
          this.countDown()
          this.$Message('发送成功')
        })
      }
    },
    countDown () {
      const clock = window.setInterval(() => {
        this.totalTime--
        this.content = `${this.totalTime}s后重新发送`
        if (this.totalTime < 0) {
          window.clearInterval(clock)
          this.content = '重新获取'
          this.totalTime = 10
          this.canClick = true
        }
      }, 1000)
    }
  }
}
</script>
<style scoped>
.sendCode {
  padding: 0 10px;
  height: 33px;
  text-align: center;
  border-radius: 8px;
  background: linear-gradient(to right, #4183b7, #4764a8);
  color: #ffffff;
  position: absolute;
  left: 185px;
  top: 0px;
}
</style>
